package com.cn.hotel.service.impl;

import com.cn.hotel.dao.INewsDao;
import com.cn.hotel.dao.Impl.NewsImpl;
import com.cn.hotel.model.NewsModel;
import com.cn.hotel.model.NewsTypeModel;
import com.cn.hotel.service.INewsService;
import redis.clients.jedis.Jedis;

import java.util.*;

public class NewsServiceImpl implements INewsService {
    /**
     * 查询所有的新闻
     *
     * @param newsModel
     * @return
     */
    @Override
    public List<NewsModel> findAll(NewsModel newsModel) {
        INewsDao iNewsDao = new NewsImpl();
        return iNewsDao.findAll(newsModel);
    }

    /**
     * 获得数据库新闻总条数
     *
     * @param newsModel
     * @return
     */
    @Override
    public int getCount(NewsModel newsModel) {
        INewsDao iNewsDao = new NewsImpl();
        return iNewsDao.getCount(newsModel);
    }

    /**
     * 添加新闻
     *
     * @param newsModel
     * @return
     */
    @Override
    public int add(NewsModel newsModel) {
        INewsDao iNewsDao = new NewsImpl();
        int result = iNewsDao.add(newsModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("newsTitle");
            jedis.del("newsId");
            jedis.del("releaseTime");
            jedis.del("news_id");
            jedis.del("news_title");
            jedis.del("release_time");
        }
        return result;
    }

    /**
     * 通过id查询当前点击的id的所有信息 给页面返回
     *
     * @param temp
     * @return
     */
    @Override
    public NewsModel findById(Integer temp) {
        INewsDao iNewsDao = new NewsImpl();
        return iNewsDao.findById(temp);
    }

    /**
     * 保存修改
     *
     * @param newsModel
     * @return
     */
    @Override
    public int newsEdit(NewsModel newsModel) {
        INewsDao iNewsDao = new NewsImpl();
        int result = iNewsDao.newsEdit(newsModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("newsTitle");
            jedis.del("newsId");
            jedis.del("releaseTime");
            jedis.del("news_id");
            jedis.del("news_title");
            jedis.del("release_time");
        }
        return result;
    }

    //进行增删改操作时，需要清楚radis存储信息  重新从数据库查询并更新缓存数据，没必要删全部，
    // 只要把判断的那个key值数据删了就可以。其他的会覆盖。
    @Override
    public int delete(Integer aa) {
        INewsDao iNewsDao = new NewsImpl();
        int result = iNewsDao.delete(aa);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("newsTitle");
            jedis.del("newsId");
            jedis.del("releaseTime");
            jedis.del("news_id");
            jedis.del("news_title");
            jedis.del("release_time");
        }
        return result;
    }

    //前端获取数据代码-----------------------------------
    @Override
    public List<NewsTypeModel> getNewsTypeModelList() {
        INewsDao iNewsDao = new NewsImpl();
        return iNewsDao.getNewsTypeModelList();
    }

    /**
     * 网站首页调取5条新闻数据
     *
     * @return
     */
    @Override
    public List<NewsModel> getHomeNewsModelList() {
        INewsDao iNewsDao = new NewsImpl();
        List<NewsModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        if (jedis.exists("newsTitle")) {
            List<String> newsId = jedis.lrange("newsId", 0, -1);
            List<String> newsTitle = jedis.lrange("newsTitle", 0, -1);
            List<String> releaseTime = jedis.lrange("releaseTime", 0, -1);
            for (int i = 0; i < newsTitle.size(); i++) {
                NewsModel model = new NewsModel();
                model.setId(Integer.parseInt(newsId.get(i)));
                model.setNewsTitle(newsTitle.get(i));
                model.setReleaseTime(releaseTime.get(i));
                list.add(model);
            }
        } else {
            list = iNewsDao.getHomeNewsModelList();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("newsId", String.valueOf(list.get(i).getId()));
                jedis.rpush("newsTitle", list.get(i).getNewsTitle());
                jedis.rpush("releaseTime", list.get(i).getReleaseTime());
            }
        }
        return list;
    }

    @Override
    public List<NewsModel> getDetails(Integer temp) {
        INewsDao iNewsDao = new NewsImpl();
        return iNewsDao.getDetails(temp);
    }

    //    public static void main(String[] args) {
//        Map<String,Integer> map = new HashMap<>();
//        map.put("temp",1);
//        map.put("pageNumber",2);
//        System.out.println(map.get("pageNumber"));
//    }
    @Override
    public int getNumber(Integer temp) {
        INewsDao iNewsDao = new NewsImpl();
        return iNewsDao.getNumber(temp);
    }

    /**
     * 新闻界面第一页存入radis，通过判断传递过来的pageNumber判断，如果是1.进行radis操作，如果不是直接从数据库查询，
     *
     * @param map
     * @return
     */
    @Override
    public List<NewsModel> getNewsModelList(Map<String, Integer> map) {
        INewsDao iNewsDao = new NewsImpl();
        List<NewsModel> list = new ArrayList<>();
//        int a = map.get("pageNumber")
        if (map.get("pageNumber") == 1&&map.get("temp")==0) {
            Jedis jedis = new Jedis();
            if (jedis.exists("news_title")) {
                List<String> news_id = jedis.lrange("news_id", 0, -1);
                List<String> news_title = jedis.lrange("news_title", 0, -1);
                List<String> release_time = jedis.lrange("release_time", 0, -1);
                List<String> news_type = jedis.lrange("news_type", 0, -1);
                for (int i = 0; i < news_id.size(); i++) {
                    NewsModel model = new NewsModel();
                    model.setId(Integer.parseInt(news_id.get(i)));
                    model.setNewsTitle(news_title.get(i));
                    model.setReleaseTime(release_time.get(i));
                    model.setNewsType(news_type.get(i));
                    list.add(model);
                }
            } else if (map.get("pageNumber") == 1&&map.get("temp")==0){
                list = iNewsDao.getNewsModelList(map);
                for (int i = 0; i < list.size(); i++) {
                    jedis.rpush("news_id", String.valueOf(list.get(i).getId()));
                    jedis.rpush("news_title", list.get(i).getNewsTitle());
                    jedis.rpush("release_time", list.get(i).getReleaseTime());
                    jedis.rpush("news_type", list.get(i).getNewsType());
                }
            }
        } else {
            list = iNewsDao.getNewsModelList(map);
        }
        return list;
    }
}

